<?php

namespace App\Events;

use App\Models\Lotto;
use App\Models\User;
use Illuminate\Broadcasting\Channel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;

class PlayerJoined implements ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    private $lotto;
    private $player;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(Lotto $lotto, User $player)
    {
        $this->player = $player;
        $this->lotto = $lotto;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('lotto.'.$this->lotto->id);
    }

    public function broadcastAs()
    {
        return 'player-joined';
    }

    public function broadcastWith()
    {
        return [
            'player' => [
                'id'       => $this->player->id,
                'avatar'   => $this->player->avatar,
                'nickname' => $this->player->nickname,
            ],
        ];
    }
}
